package com.tuanzi.loan.core.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @author Ray 2018/01/25
 */
public class TimeUtils {

	public static String formatDuring(Long duration) {
		return formatDuring(duration, false);
	}

	public static String formatDuring(Long duration, boolean showMillis) {
		if (duration == null) {
			return StringUtils.EMPTY;
		}
		long days = duration / (1000 * 60 * 60 * 24);
		long hours = (duration % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (duration % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (duration % (1000 * 60)) / 1000;
		long millis = duration % 1000;

		StringBuilder sb = new StringBuilder();
		if (days > 0) {
			sb.append(days).append("天");
		}
		if (hours > 0 || (days > 0 && hours == 0)) {
			sb.append(hours).append("小时");
		}
		if (minutes > 0 || (hours > 0 && minutes == 0) || (days > 0 && hours == 0)) {
			sb.append(minutes).append("分");
		}

		sb.append(seconds);
		if (showMillis) {
			sb.append(".").append(millis);
		}

		sb.append("秒");
		return sb.toString();
	}
}
